package platform;

import io.Packet;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.transport.socket.nio.NioSocketConnector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import protomsg.Login.LoginMsg;
import protomsg.Login.LoginMsgOrBuilder;

public class LogicServerIoHandler extends IoHandlerAdapter{
	protected static final Logger log = LoggerFactory.getLogger(LogicServerIoHandler.class);
	private NioSocketConnector conn;
	public LogicServerIoHandler(NioSocketConnector con)
	{
		conn = con;
	}
	
	@Override
	public void sessionClosed(IoSession arg0) throws Exception {
		log.info("IoHandlerAdapter sessionClosed!");
	}
	
	@Override
	public void messageReceived(IoSession arg0, Object msg) throws Exception {
		//log.info("IoHandlerAdapter messageReceived!");
		
		
		
		if(msg instanceof Packet) {
//			log.info("recv msg from logic server:" + 
//		conn.getDefaultRemoteAddress().getHostString() + ":" + conn.getDefaultRemoteAddress().getPort());
			LogicServerConnect.handleMsg((Packet)msg, arg0);
			}
		else if(msg instanceof byte[]){
			
			byte[] bytes = (byte[])msg;
			LoginMsg data = LoginMsg.parseFrom(bytes);
			
			System.out.println(data.getUserID() + ":" + data.getUserPwd());
		}
		else
		{
			log.info("recv msg is not a Packet!!!");
		}
	}
	
	@Override
	public void exceptionCaught(IoSession arg0, Throwable arg1) throws Exception {
		arg1.printStackTrace();
	}

}
